from rest_framework import serializers
from ..models.Department import Department
from ..models.Institution import Institution
from ..serializers.instListSerializer import InstListSerializer

class DepartCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = ["name", "zh_name", "dep_code", "contact_number", "email", "website", "founded_date", "description", "dep_type", "institution"]
    
    
    def create(self, validated_data):
        a = Department.objects.create(**validated_data)
        return a
    
    def update(self, instance, validated_data):
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance
        
class DepInfoSerializer(serializers.ModelSerializer):
    institution = serializers.SerializerMethodField()
    
    class Meta:
        model = Department
        fields = ["uuid", "name", "zh_name", "dep_code", "contact_number", "email", \
                  "website", "founded_date", "description", "dep_type", 'institution']
        
    def get_institution(self, obj):
        return InstListSerializer(obj.institution).data